//
//  DingDanTopView.m
//  Experss
//
//  Created by Chen on 2017/8/3.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "DingDanTopView.h"

@interface DingDanTopView ()

/**  */
@property (nonatomic , strong) NSMutableArray *btnArray;

@end

@implementation DingDanTopView

+ (instancetype)initWithDingDanTopView{
    DingDanTopView *view = [[DingDanTopView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 40)];
    
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.btnArray = [NSMutableArray array];
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews{
    NSArray *titleArray = @[@"全部",@"待付款",@"待发货",@"待收货",@"待评价",@"退换货"];
    
    CGFloat w = (SCREEN_WIDTH - titleArray.count*2*7)/titleArray.count;
    
    for (NSInteger i = 0; i < titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(7*(2*i+1)+i*w, 7, w, 26 *(kScreen_Width/375));
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:titleArray[i] forState:(UIControlStateNormal)];
        [btn setTitle:titleArray[i] forState:(UIControlStateSelected)];
        btn.titleLabel.font = [UIFont systemFontOfSize:14 *(kScreen_Width/375)];
        [btn setTitleColor:[UIColor colorWithString:@"333333"] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"fenlei_kuai"] forState:(UIControlStateSelected)];
        [btn setBackgroundImage:nil forState:(UIControlStateNormal)];
        btn.tag = 3000 + i;
        btn.layer.cornerRadius = 26 *(kScreen_Width/375)/2;
        btn.clipsToBounds = YES;
        [btn addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:btn];
        
        [self.btnArray addObject:btn];
    }

    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kScreen_Width, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
}

- (void)selectAction:(UIButton *)btn{
    if (btn.selected == YES) {
        return;
    }
    
    NSInteger index = btn.tag - 3000;
    
    btn.selected = YES;

    
    
    
    for (UIButton *tempBtn in self.btnArray) {
        if (tempBtn != btn) {
            tempBtn.selected = NO;

        }
    }
    
    if ([self.delegate respondsToSelector:@selector(dingdanTopSelectWithIndex:)]) {
        [self.delegate dingdanTopSelectWithIndex:index];
    }
    
}

- (void)setIndex:(NSInteger)index{
    _index = index;
    
    [self selectAction:self.btnArray[index]];
}

@end
